package com.zust.data.serivce;

import com.zust.data.dal.DlChartRepository;
import com.zust.data.dal.DlColumnRepository;
import com.zust.data.dal.DlDataRepository;
import com.zust.data.dal.UserRepository;
import com.zust.data.domain.param.ChartSaveParam;
import com.zust.data.dto.ResponseDto;
import com.zust.data.entity.DlChart;
import com.zust.data.entity.DlData;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.inject.Inject;
import java.util.List;

@Service
public class ChartService {

    @Inject
    private UserRepository userRepository;

    @Inject
    private DlDataRepository dlDataRepository;

    @Inject
    private DlColumnRepository dlColumnRepository;

    @Inject
    private DlChartRepository dlChartRepository;


    public ResponseDto save(ChartSaveParam chartSaveParam) {
        if (StringUtils.isNotBlank(chartSaveParam.getId())) {

        } else {
            DlData dlData = dlDataRepository.findOne(chartSaveParam.getDataId());
            DlChart dlChart = new DlChart();
            BeanUtils.copyProperties(chartSaveParam, dlChart);
            dlChart.setTitle(dlData.getTitle());
            dlChart.setIntro(dlData.getIntro());
            dlChart.setIsShow(dlData.getIsShow());
            return new ResponseDto(true, "操作成功", dlChartRepository.save(dlChart));
        }
        return new ResponseDto(false);

    }

    public List<DlChart> list(String userId) {
        List<DlChart> list = dlChartRepository.findAllByUserIdAndDeletedFlag(userId, 0);
        return list;
    }

    public DlChart info(String chartId) {
        return dlChartRepository.findOne(chartId);
    }

    public void deleted(String chartId) {
        DlChart chart = dlChartRepository.findOne(chartId);
        chart.setDeletedFlag(1);
        dlChartRepository.save(chart);
    }
    public void update(String chartId, String title, Integer isShow, String intro) {
        DlChart chart = dlChartRepository.findOne(chartId);
        chart.setTitle(title);
        chart.setIntro(intro);
        chart.setIsShow(isShow);
        dlChartRepository.save(chart);
    }

}
